﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using Foundation;

namespace BusinessEntity
{
    public class Utils
    {
        public static void ReadRegistry(out string lsEncUsername, out string lsEncPassword)
        {
            RegistryKey lRegistryKey = Registry.CurrentUser.OpenSubKey(Constant.GDocsMobileRegistryRoot);

            try
            {
                if (lRegistryKey == null)
                {
                    lRegistryKey = Registry.CurrentUser.CreateSubKey(Constant.GDocsMobileRegistryRoot);
                }

                lsEncUsername = "" + (string)lRegistryKey.GetValue("username");
                lsEncPassword = "" + (string)lRegistryKey.GetValue("password");
            }
            finally
            {
                lRegistryKey.Close();
            }
        }

        public static void WriteRegistry(string lsEncUsername, string lsEncPassword)
        {
            RegistryKey lRegistryKey = Registry.CurrentUser.OpenSubKey(Constant.GDocsMobileRegistryRoot, true);

            try
            {

                if (lRegistryKey == null)
                {
                    lRegistryKey = Registry.CurrentUser.CreateSubKey(Constant.GDocsMobileRegistryRoot);
                }

                lRegistryKey.SetValue("username", lsEncUsername);
                lRegistryKey.SetValue("password", lsEncPassword);
            }
            finally
            {
                lRegistryKey.Close();
            }
        }

        public static bool ClearOptions()
        {
            bool lbRet = false;

            RegistryKey lRegistryKey = Registry.CurrentUser.OpenSubKey(Constant.GDocsMobileRegistryRoot, true);

            try
            {
                lRegistryKey.DeleteValue("username", false);
                lRegistryKey.DeleteValue("password", false);

                lbRet = true;
            }
            catch (Exception ex)
            {
                lbRet = false;
                throw ex;
            }
            finally
            {
                lRegistryKey.Close();
            }
            return lbRet;

        }

    }
}
